
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>16. 附录 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="Python安装和使用" href="../using/index.html" />
    <link rel="prev" title="15. 浮点算术：争议和限制" href="floatingpoint.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/tutorial/appendix.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="../using/index.html" title="Python安装和使用"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="floatingpoint.html" title="15. 浮点算术：争议和限制"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python 教程</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="appendix">
<span id="tut-appendix"></span><h1><span class="section-number">16. </span>附录<a class="headerlink" href="#appendix" title="永久链接至标题">¶</a></h1>
<div class="section" id="interactive-mode">
<span id="tut-interac"></span><h2><span class="section-number">16.1. </span>交互模式<a class="headerlink" href="#interactive-mode" title="永久链接至标题">¶</a></h2>
<div class="section" id="error-handling">
<span id="tut-error"></span><h3><span class="section-number">16.1.1. </span>错误处理<a class="headerlink" href="#error-handling" title="永久链接至标题">¶</a></h3>
<p>当发生错误时，解释器会打印错误信息和错误堆栈。在交互模式下，将返回到主命令提示符；如果输入内容来自文件，在打印错误堆栈之后，程序会以非零状态退出。（这里所说的错误不包括 <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> 语句中由 <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> 所捕获的异常。）有些错误是无条件致命的，会导致程序以非零状态退出；比如内部逻辑矛盾或内存耗尽。所有错误信息都会被写入标准错误流；而命令的正常输出则被写入标准输出流。</p>
<p>将中断字符（通常为 <kbd class="kbd docutils literal notranslate">Control-C</kbd> 或 <kbd class="kbd docutils literal notranslate">Delete</kbd> ）键入主要或辅助提示会取消输入并返回主提示符。 <a class="footnote-reference brackets" href="#id2" id="id1">1</a> 在执行命令时键入中断引发的 <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> 异常，可以由 <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> 语句处理。</p>
</div>
<div class="section" id="executable-python-scripts">
<span id="tut-scripts"></span><h3><span class="section-number">16.1.2. </span>可执行的Python脚本<a class="headerlink" href="#executable-python-scripts" title="永久链接至标题">¶</a></h3>
<p>在BSD等类Unix系统上，Python脚本可以直接执行，就像shell脚本一样，第一行添加:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python3.5</span>
</pre></div>
</div>
<p>（假设解释器位于用户的 <span class="target" id="index-4"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> ）脚本的开头，并将文件设置为可执行。 <code class="docutils literal notranslate"><span class="pre">#!</span></code> 必须是文件的前两个字符。在某些平台上，第一行必须以Unix样式的行结尾（<code class="docutils literal notranslate"><span class="pre">'\n'</span></code>）结束，而不是以Windows（<code class="docutils literal notranslate"><span class="pre">'\r\n'</span></code>）行结尾。请注意，散列或磅字符 <code class="docutils literal notranslate"><span class="pre">'#'</span></code> 在Python中代表注释开始。</p>
<p>可以使用 <strong class="program">chmod</strong> 命令为脚本提供可执行模式或权限。</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> chmod +x myscript.py
</pre></div>
</div>
<p>在Windows系统上，没有“可执行模式”的概念。 Python安装程序自动将 <code class="docutils literal notranslate"><span class="pre">.py</span></code> 文件与 <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> 相关联，这样双击Python文件就会将其作为脚本运行。扩展也可以是 <code class="docutils literal notranslate"><span class="pre">.pyw</span></code> ，在这种情况下，会隐藏通常出现的控制台窗口。</p>
</div>
<div class="section" id="the-interactive-startup-file">
<span id="tut-startup"></span><h3><span class="section-number">16.1.3. </span>交互式启动文件<a class="headerlink" href="#the-interactive-startup-file" title="永久链接至标题">¶</a></h3>
<p>当您以交互方式使用Python时，每次启动解释器时都会执行一些标准命令，这通常很方便。您可以通过将名为 <span class="target" id="index-5"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code></a> 的环境变量设置为包含启动命令的文件名来实现。这类似于Unix shell的 <code class="file docutils literal notranslate"><span class="pre">.profile</span></code> 功能。</p>
<p>This file is only read in interactive sessions, not when Python reads commands
from a script, and not when <code class="file docutils literal notranslate"><span class="pre">/dev/tty</span></code> is given as the explicit source of
commands (which otherwise behaves like an interactive session).  It is executed
in the same namespace where interactive commands are executed, so that objects
that it defines or imports can be used without qualification in the interactive
session. You can also change the prompts <code class="docutils literal notranslate"><span class="pre">sys.ps1</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.ps2</span></code> in this
file.</p>
<p>如果你想从当前目录中读取一个额外的启动文件，你可以使用像 <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">os.path.isfile('.pythonrc.py'):</span> <span class="pre">exec(open('.pythonrc.py').read())</span></code> 这样的代码在全局启动文件中对它进行编程。如果要在脚本中使用启动文件，则必须在脚本中显式执行此操作:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;PYTHONSTARTUP&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">filename</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="k">as</span> <span class="n">fobj</span><span class="p">:</span>
        <span class="n">startup_file</span> <span class="o">=</span> <span class="n">fobj</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="n">exec</span><span class="p">(</span><span class="n">startup_file</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="the-customization-modules">
<span id="tut-customize"></span><h3><span class="section-number">16.1.4. </span>定制模块<a class="headerlink" href="#the-customization-modules" title="永久链接至标题">¶</a></h3>
<p>Python提供了两个钩子来让你自定义它：<code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code> 和 <code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</span></code>。要查看其工作原理，首先需要找到用户site-packages目录的位置。启动Python并运行此代码:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">site</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">site</span><span class="o">.</span><span class="n">getusersitepackages</span><span class="p">()</span>
<span class="go">&#39;/home/user/.local/lib/python3.5/site-packages&#39;</span>
</pre></div>
</div>
<p>现在，您可以在该目录中创建一个名为 <code class="file docutils literal notranslate"><span class="pre">usercustomize.py</span></code> 的文件，并将所需内容放入其中。它会影响Python的每次启动，除非它以 <a class="reference internal" href="../using/cmdline.html#cmdoption-s"><code class="xref std std-option docutils literal notranslate"><span class="pre">-s</span></code></a> 选项启动，以禁用自动导入。</p>
<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code> 以相同的方式工作，但通常由计算机管理员在全局 site-packages 目录中创建，并在 <code class="xref py py-mod docutils literal notranslate"><span class="pre">usercustomize</span></code> 之前被导入。有关详情请参阅 <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> 模块的文档。</p>
<p class="rubric">备注</p>
<dl class="footnote brackets">
<dt class="label" id="id2"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p>GNU Readline 包的问题可能会阻止这种情况。</p>
</dd>
</dl>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#">16. 附录</a><ul>
<li><a class="reference internal" href="#interactive-mode">16.1. 交互模式</a><ul>
<li><a class="reference internal" href="#error-handling">16.1.1. 错误处理</a></li>
<li><a class="reference internal" href="#executable-python-scripts">16.1.2. 可执行的Python脚本</a></li>
<li><a class="reference internal" href="#the-interactive-startup-file">16.1.3. 交互式启动文件</a></li>
<li><a class="reference internal" href="#the-customization-modules">16.1.4. 定制模块</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="floatingpoint.html"
                        title="上一章"><span class="section-number">15. </span>浮点算术：争议和限制</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="../using/index.html"
                        title="下一章">Python安装和使用</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/tutorial/appendix.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="../using/index.html" title="Python安装和使用"
             >下一页</a> |</li>
        <li class="right" >
          <a href="floatingpoint.html" title="15. 浮点算术：争议和限制"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 教程</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>